package com.pp.pingpong;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class ConfigurationActivity extends Activity{
	
	public static final String PREFS_NAME = "PingPong";
	
	EditText ip;
	EditText port;
	EditText nick;
	
	Button save;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {	
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.configuration_activity);
		
		ip = (EditText) findViewById(R.id.ipAddress);
		port = (EditText) findViewById(R.id.portNumber);
		nick = (EditText) findViewById(R.id.nickName);
		
		save = (Button) findViewById(R.id.button1);
		
		LoadConfiguration();
		
		ip.setText( Config.getInstance().getIpAddress());
		port.setText( Config.getInstance().getPortNumber());
		nick.setText( Config.getInstance().getNickName());
		
		save.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				SaveConfiguration(ip.getText().toString().trim(),port.getText().toString().trim(),nick.getText().toString().trim());	
			}
		});		
	}	
	
	public void LoadConfiguration() {		
		SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);	    
	    Config.getInstance().setIpAddress(settings.getString("ipAddress", ""));
	    Config.getInstance().setPortNumber(settings.getString("portNumber", ""));
	    Config.getInstance().setNickName(settings.getString("nickName", ""));	
	}
	
	public void SaveConfiguration(String ip, String port, String nick) {
		
		SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);	
		SharedPreferences.Editor editor = settings.edit();
		editor.putString("ipAddress", ip);
		editor.putString("portNumber", port);
		editor.putString("nickName", nick);
		editor.commit();
	    
	
	}
}
